<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>animatedzippy.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_animatedzippy.js.html">animatedzippy.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Animated zippy widget implementation.
<a name="line17"></a> *
<a name="line18"></a> * @author eae@google.com (Emil A Eklund)
<a name="line19"></a> * @see ../demos/zippy.html
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.ui.AnimatedZippy&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.dom&#39;);
<a name="line25"></a>goog.require(&#39;goog.events&#39;);
<a name="line26"></a>goog.require(&#39;goog.fx.Animation&#39;);
<a name="line27"></a>goog.require(&#39;goog.fx.Animation.EventType&#39;);
<a name="line28"></a>goog.require(&#39;goog.fx.Transition.EventType&#39;);
<a name="line29"></a>goog.require(&#39;goog.fx.easing&#39;);
<a name="line30"></a>goog.require(&#39;goog.ui.Zippy&#39;);
<a name="line31"></a>goog.require(&#39;goog.ui.ZippyEvent&#39;);
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>
<a name="line35"></a>/**
<a name="line36"></a> * Zippy widget. Expandable/collapsible container, clicking the header toggles
<a name="line37"></a> * the visibility of the content.
<a name="line38"></a> *
<a name="line39"></a> * @param {Element|string|null} header Header element, either element
<a name="line40"></a> *     reference, string id or null if no header exists.
<a name="line41"></a> * @param {Element|string} content Content element, either element reference or
<a name="line42"></a> *     string id.
<a name="line43"></a> * @param {boolean=} opt_expanded Initial expanded/visibility state. Defaults to
<a name="line44"></a> *     false.
<a name="line45"></a> * @constructor
<a name="line46"></a> * @extends {goog.ui.Zippy}
<a name="line47"></a> */
<a name="line48"></a>goog.ui.AnimatedZippy = function(header, content, opt_expanded) {
<a name="line49"></a>  // Create wrapper element and move content into it.
<a name="line50"></a>  var elWrapper = goog.dom.createDom(&#39;div&#39;, {&#39;style&#39;: &#39;overflow:hidden&#39;});
<a name="line51"></a>  var elContent = goog.dom.getElement(content);
<a name="line52"></a>  elContent.parentNode.replaceChild(elWrapper, elContent);
<a name="line53"></a>  elWrapper.appendChild(elContent);
<a name="line54"></a>
<a name="line55"></a>  /**
<a name="line56"></a>   * Content wrapper, used for animation.
<a name="line57"></a>   * @type {Element}
<a name="line58"></a>   * @private
<a name="line59"></a>   */
<a name="line60"></a>  this.elWrapper_ = elWrapper;
<a name="line61"></a>
<a name="line62"></a>  /**
<a name="line63"></a>   * Reference to animation or null if animation is not active.
<a name="line64"></a>   * @type {goog.fx.Animation}
<a name="line65"></a>   * @private
<a name="line66"></a>   */
<a name="line67"></a>  this.anim_ = null;
<a name="line68"></a>
<a name="line69"></a>  // Call constructor of super class.
<a name="line70"></a>  goog.ui.Zippy.call(this, header, elContent, opt_expanded);
<a name="line71"></a>
<a name="line72"></a>  // Set initial state.
<a name="line73"></a>  // NOTE: Set the class names as well otherwise animated zippys
<a name="line74"></a>  // start with empty class names.
<a name="line75"></a>  var expanded = this.isExpanded();
<a name="line76"></a>  this.elWrapper_.style.display = expanded ? &#39;&#39; : &#39;none&#39;;
<a name="line77"></a>  this.updateHeaderClassName(expanded);
<a name="line78"></a>};
<a name="line79"></a>goog.inherits(goog.ui.AnimatedZippy, goog.ui.Zippy);
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/**
<a name="line83"></a> * Duration of expand/collapse animation, in milliseconds.
<a name="line84"></a> * @type {number}
<a name="line85"></a> */
<a name="line86"></a>goog.ui.AnimatedZippy.prototype.animationDuration = 500;
<a name="line87"></a>
<a name="line88"></a>
<a name="line89"></a>/**
<a name="line90"></a> * Acceleration function for expand/collapse animation.
<a name="line91"></a> * @type {!Function}
<a name="line92"></a> */
<a name="line93"></a>goog.ui.AnimatedZippy.prototype.animationAcceleration = goog.fx.easing.easeOut;
<a name="line94"></a>
<a name="line95"></a>
<a name="line96"></a>/**
<a name="line97"></a> * @return {boolean} Whether the zippy is in the process of being expanded or
<a name="line98"></a> *     collapsed.
<a name="line99"></a> */
<a name="line100"></a>goog.ui.AnimatedZippy.prototype.isBusy = function() {
<a name="line101"></a>  return this.anim_ != null;
<a name="line102"></a>};
<a name="line103"></a>
<a name="line104"></a>
<a name="line105"></a>/**
<a name="line106"></a> * Sets expanded state.
<a name="line107"></a> *
<a name="line108"></a> * @param {boolean} expanded Expanded/visibility state.
<a name="line109"></a> * @override
<a name="line110"></a> */
<a name="line111"></a>goog.ui.AnimatedZippy.prototype.setExpanded = function(expanded) {
<a name="line112"></a>  if (this.isExpanded() == expanded &amp;&amp; !this.anim_) {
<a name="line113"></a>    return;
<a name="line114"></a>  }
<a name="line115"></a>
<a name="line116"></a>  // Reset display property of wrapper to allow content element to be
<a name="line117"></a>  // measured.
<a name="line118"></a>  if (this.elWrapper_.style.display == &#39;none&#39;) {
<a name="line119"></a>    this.elWrapper_.style.display = &#39;&#39;;
<a name="line120"></a>  }
<a name="line121"></a>
<a name="line122"></a>  // Measure content element.
<a name="line123"></a>  var h = this.getContentElement().offsetHeight;
<a name="line124"></a>
<a name="line125"></a>  // Stop active animation (if any) and determine starting height.
<a name="line126"></a>  var startH = 0;
<a name="line127"></a>  if (this.anim_) {
<a name="line128"></a>    expanded = this.isExpanded();
<a name="line129"></a>    goog.events.removeAll(this.anim_);
<a name="line130"></a>    this.anim_.stop(false);
<a name="line131"></a>
<a name="line132"></a>    var marginTop = parseInt(this.getContentElement().style.marginTop, 10);
<a name="line133"></a>    startH = h - Math.abs(marginTop);
<a name="line134"></a>  } else {
<a name="line135"></a>    startH = expanded ? 0 : h;
<a name="line136"></a>  }
<a name="line137"></a>
<a name="line138"></a>  // Updates header class name after the animation has been stopped.
<a name="line139"></a>  this.updateHeaderClassName(expanded);
<a name="line140"></a>
<a name="line141"></a>  // Set up expand/collapse animation.
<a name="line142"></a>  this.anim_ = new goog.fx.Animation([0, startH],
<a name="line143"></a>                                     [0, expanded ? h : 0],
<a name="line144"></a>                                     this.animationDuration,
<a name="line145"></a>                                     this.animationAcceleration);
<a name="line146"></a>
<a name="line147"></a>  var events = [goog.fx.Transition.EventType.BEGIN,
<a name="line148"></a>                goog.fx.Animation.EventType.ANIMATE,
<a name="line149"></a>                goog.fx.Transition.EventType.END];
<a name="line150"></a>  goog.events.listen(this.anim_, events, this.onAnimate_, false, this);
<a name="line151"></a>  goog.events.listen(this.anim_,
<a name="line152"></a>                     goog.fx.Transition.EventType.END,
<a name="line153"></a>                     goog.bind(this.onAnimationCompleted_, this, expanded));
<a name="line154"></a>
<a name="line155"></a>  // Start animation.
<a name="line156"></a>  this.anim_.play(false);
<a name="line157"></a>};
<a name="line158"></a>
<a name="line159"></a>
<a name="line160"></a>/**
<a name="line161"></a> * Called during animation
<a name="line162"></a> *
<a name="line163"></a> * @param {goog.events.Event} e The event.
<a name="line164"></a> * @private
<a name="line165"></a> */
<a name="line166"></a>goog.ui.AnimatedZippy.prototype.onAnimate_ = function(e) {
<a name="line167"></a>  var contentElement = this.getContentElement();
<a name="line168"></a>  var h = contentElement.offsetHeight;
<a name="line169"></a>  contentElement.style.marginTop = (e.y - h) + &#39;px&#39;;
<a name="line170"></a>};
<a name="line171"></a>
<a name="line172"></a>
<a name="line173"></a>/**
<a name="line174"></a> * Called once the expand/collapse animation has completed.
<a name="line175"></a> *
<a name="line176"></a> * @param {boolean} expanded Expanded/visibility state.
<a name="line177"></a> * @private
<a name="line178"></a> */
<a name="line179"></a>goog.ui.AnimatedZippy.prototype.onAnimationCompleted_ = function(expanded) {
<a name="line180"></a>  // Fix wrong end position if the content has changed during the animation.
<a name="line181"></a>  if (expanded) {
<a name="line182"></a>    this.getContentElement().style.marginTop = &#39;0&#39;;
<a name="line183"></a>  }
<a name="line184"></a>
<a name="line185"></a>  goog.events.removeAll(this.anim_);
<a name="line186"></a>  this.setExpandedInternal(expanded);
<a name="line187"></a>  this.anim_ = null;
<a name="line188"></a>
<a name="line189"></a>  if (!expanded) {
<a name="line190"></a>    this.elWrapper_.style.display = &#39;none&#39;;
<a name="line191"></a>  }
<a name="line192"></a>
<a name="line193"></a>  // Fire toggle event.
<a name="line194"></a>  this.dispatchEvent(new goog.ui.ZippyEvent(goog.ui.Zippy.Events.TOGGLE,
<a name="line195"></a>                                            this, expanded));
<a name="line196"></a>};
</pre>


</body>
</html>
